define(['jquery','echarts',
	"scprit/followup/app/simpleMR/simpleMR.servers.js",
	"scprit/followup/app/simpleMR/simpleMR.ui.js",
	"scprit/followup/app/simpleMR/simpleMR.event.js"],
	function($,echarts,$specialtyBagServers,$specialtyBagUi,$specialtyBagEvent){
	var specialtyBag = {};

	/**
	 * 饼状图
	 */
	var drawCanvas = function(id,options){
		var countCard = document.getElementById(id);  //$("#"+id);
        var myChart = echarts.init(countCard);
        var option = {
		    tooltip: {
		        trigger: 'item',
		        formatter: "{b}: {c} ({d}%)"
		    },
		    legend: {
		        orient: 'vertical',
		        top: '20%',
		        right:'1%',
		        data:['正常','失访','其他']
		    },
		    series: [
		        {
		            name:'',
		            type:'pie',
		            radius: ['50%', '70%'],
		            center: ['30%','50%'],
		            avoidLabelOverlap: false,
		            label: {
		                normal: {
		                    show: false,
		                }
		            },
		            labelLine: {
		                normal: {
		                    show: false
		                }
		            },
		            data:[
		                {
		                	value:options.completeNum, 
		                	name:'正常',
		                	label: {
				                normal: {
                                    show: true,
                                   	position: 'center',
                                    formatter:'随访总数 \n',
                                    textStyle:{
                                        fontSize: 15,
                                        color:'#4CA7FF'
                                    }
                                }
				            },
		                },
		                {
		                	value:options.lostNum, 
		                	name:'失访',
		                	label: {
				                normal: {
                                    show: true,
                                    position: 'center',
                                    formatter:'\n\n'+options.totalNum+'次',
                                    textStyle:{
                                        fontSize: 14,
                                        color:'#7a7a7a'
                                    }
                                }
				            },
		                },
		                {value:options.other, name:'其他'}
		            ]
		        }
		    ]
		};
		myChart.setOption(option);
	};

	/**
	 * 患者基本信息
	 */
	var infoInit = function(curpatient){
		$specialtyBagUi.basicInfoInit({
			id:"basicInfoCard",
			list: curpatient
		});
		var other = curpatient.totalNum - curpatient.completeNum - curpatient.lostNum;
		drawCanvas("countCard",{
			completeNum:curpatient.completeNum,
			lostNum:curpatient.lostNum,
			other:other,
			totalNum:curpatient.totalNum,
		});
	}

	/**
	 * 请求Category详情成功后的回调函数
	 */
	var ajaxCategoryDetailCallBack = function(data){
		$specialtyBagUi.theadInit({
			id:"thead",
			list: data.tasks
		});
		$specialtyBagUi.tbodyInit({
			id:"tbody",
			list: data.questionnaireData,
			task: data.tasks,
			status: "readonly"
		});
	}

	/**
	 * 安医大新增（数据暂时是假的）
	 */
	var ajaxCategoryDetailCallBack2 = function(data){
		if(data.sampleList && jQuery.isArray(data.sampleList)){
			var sampleData = $specialtyBagServers.getSampleData(data.sampleList);
			$specialtyBagUi.theadInit({
				id:"thead",
				list: sampleData.sampleValue
			});
			$specialtyBagUi.tbodyInit({
				id:"tbody",
				list: sampleData.sampleQuest,
				task: sampleData.sampleValue,
				status: "readonly"
			});
		}
	}

	/**
	 * 请求患者的categoryList成功后的回调函数
	 */
	var ajaxCategoryListCallBack = function(params,rows){
		$specialtyBagUi.categoryListInit({
			id:"categoryList",
			list: rows
		});
		// 点击箭头滚动
		$specialtyBagEvent.onArrowClick();
		$("#categoryList > div.item,#categoryList > div.collect").unbind("click");
		$("#categoryList > div.item").data({"data":params,"callback":ajaxCategoryDetailCallBack});
		$("#categoryList > div.collect").data({"data":params,"callback":ajaxCategoryDetailCallBack2});
		$("#categoryList > div.item,#categoryList > div.collect").click($specialtyBagEvent.onCategoryClick);
		$("#categoryList > div.item").first().trigger("click"); // 默认点击第一个
	}

	/**
	 * 右侧
	 */
	var specialtyBagRight = function(curpatient){
		infoInit(curpatient);
		var params = {
			"patientId":curpatient.patientId,
			"patientSchemeId":curpatient.patientSchemeId
		};
		$specialtyBagServers.ajaxCategoryList(params,ajaxCategoryListCallBack);
	}
	
	/**
	 * 请求患者列表成功后的回调函数
	 */
	var ajaxPatientsCallBack = function(rows){
		if(rows && rows.length > 0){
			$specialtyBagUi.listInit({
				id:"baby",
				list: rows
			});
			// 初始化
			$("#baby .babyList").eq(0).addClass("active").siblings().removeClass("active");
			specialtyBagRight(rows[0]);
			// 点击
			$("#baby .babyList").unbind("click");
			$("#baby .babyList").click(function(){
				$(this).addClass("active").siblings().removeClass("active");
				var index = $(this).index();
				specialtyBagRight(rows[index]);
			});
		}else{
			$("#baby").html('<div class="col-sm-12 col-md-12 baby-null">暂无数据..</div>');
		}
	}

	specialtyBag.init = function(){
		$specialtyBagEvent.onResizeClick();
		$specialtyBagServers.ajaxPatients("",ajaxPatientsCallBack);
		// 根据姓名搜索
		$(".search-box #search").bind("input propertychange",function(){
			var keyword = $(this).val();
			$specialtyBagServers.ajaxPatients(keyword,ajaxPatientsCallBack);
		});
	};

	return specialtyBag;
});